package com.yhq.spring.websocket.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 商汤认证token工具类
 */
@Slf4j
public final class STAuthenticationTokenUtils {
    public static void main(String[] args) {
        String ak = "54D024C60B864E8794F7569A5C82C22F"; // 填写您的ak 访问账号
        String sk = "AF291D609F674B51BA44ACEC52654433"; // 填写您的sk 访问密钥
        String token = sign(ak, sk);
        System.out.println(token); // 打印生成的API_TOKEN
    }
    public static String sign(String ak,String sk) {
        Date expiredAt = new Date(System.currentTimeMillis() + 1800*1000);
        Date notBefore = new Date(System.currentTimeMillis() - 5*1000);
        Algorithm algo = Algorithm.HMAC256(sk);
        Map<String, Object> header = new HashMap<>();
        header.put("alg", "HS256");
        return JWT.create()
                .withIssuer(ak)
                .withHeader(header)
                .withExpiresAt(expiredAt)
                .withNotBefore(notBefore)
                .sign(algo);
    }
}